package com.tust.service;

import com.tust.bean.StudentCourse;
import com.tust.bean.TeachCourse;
import com.tust.dao.StudentCourseDao;
import com.tust.dao.TeachCourseDao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class StudentCourseServiceImpl implements BaseService{

    @Override
    public void service(Connection connection) {

        TeachCourseDao teachCourseDao = new TeachCourseDao(connection);

        Scanner scanner = new Scanner(System.in);
        String type;

        List<StudentCourse> list;
        List<TeachCourse> listTeacherCourse;

        while (true) {
            System.out.println("请输入学生选课管理操作命令：add->添加，edit->编辑,del->删除,Query->查询，QUIT->退出");
            StudentCourseDao studentCourseDao = new StudentCourseDao(connection);
            type = scanner.next();

            if (type.equalsIgnoreCase("add")) {

                 listTeacherCourse = teachCourseDao.query();
                 new TeacherCourseServiceImpl().print(listTeacherCourse);

                System.out.println("请依次单独输入要添加选课信息的学生学号、授课记录所在行的ID：");
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setSid(scanner.nextInt());
                studentCourse.setTcid(scanner.nextInt());
                if (studentCourseDao.insert(studentCourse)) {
                    System.out.println("添加成功");
                    list = studentCourseDao.query();
                    print(list);
                } else {
                    System.out.println("添加失败");
                }
            } else if (type.equalsIgnoreCase("del")) {

                // 打印选课记录表
                list = studentCourseDao.query();
                print(list);

                System.out.println("删除指定学生选课记录请输入学号、删除所有学生的所有选课记录请输入alls");
                StudentCourse studentCourse = new StudentCourse();
                type = scanner.next();
                if (!type.equalsIgnoreCase("alls")) {
                    studentCourse.setSid(Integer.parseInt(type));
                    System.out.println("删除当前学生的指定课程请输入选课ID，删除当前学生的所有课程请输入all：");
                    type = scanner.next();

                }

                if (type.equalsIgnoreCase("alls")) {
                    if (studentCourseDao.delete()) {
                        System.out.println("所有学生的所有选课记录删除成功！");
                    } else {
                        System.out.println("所有学生的所有选课记录删除失败！");
                    }
                } else if(type.equalsIgnoreCase("all")) {

                    if (studentCourseDao.delete(studentCourse.getSid())) {
                        System.out.println("当前学生的所有选课记录删除成功！");
                        list = studentCourseDao.query();
                        print(list);
                    } else {
                        System.out.println("当前学生的所有选课记录删除失败！");
                    }
                } else {
                    studentCourse.setScid(Integer.parseInt(type));
                    if (studentCourseDao.deleteById(studentCourse.getScid())) {
                        System.out.println("当前学生的选课ID为" + type + "的课程选课信息删除成功！");
                        list = studentCourseDao.query();
                        print(list);
                    } else {
                        System.out.println("当前学生的选课ID为" + type + "的课程选课信息删除失败！");
                    }
                }
            } else if (type.equalsIgnoreCase("edit")) {
                System.out.println("----------- 所有课程的授课表 ----------");
                listTeacherCourse = teachCourseDao.query();
                new TeacherCourseServiceImpl().print(listTeacherCourse);
                System.out.println("--------------------- 选课信息表 --------------------");
                // 打印选课记录
                list = studentCourseDao.query();
                print(list);

                System.out.println("请依次单独输入要更新选课信息表的id、学生学号、授课表ID：");
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setScid(scanner.nextInt());
                studentCourse.setSid(scanner.nextInt());
                studentCourse.setTcid(scanner.nextInt());
                if (studentCourseDao.update(studentCourse)) {
                    System.out.println("更新成功");
                    list = studentCourseDao.query();
                    print(list);
                } else {
                    System.out.println("更新失败");
                }
            } else if (type.equalsIgnoreCase("query")) {
                System.out.println("查询指定学生请输入学生学号，查询所有学生输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    list = studentCourseDao.query();
                    print(list);
                } else {
                    list = studentCourseDao.query(Integer.parseInt(type));
                    print(list);
                }
            } else if (type.equalsIgnoreCase("QUIT")) {
                studentCourseDao.close();
                break;
            }
        }

    }

    @Override
    public void print(List list) {

        if (list.size() > 0) {
            Iterator<StudentCourse> iterator = list.iterator();
            System.out.println("id\t学生学号\t  学生姓名\t教师号\t教师名\t课程号\t课程名");
            while (iterator.hasNext()) {
                StudentCourse studentCourse = iterator.next();
                String id = String.format("%-4s", studentCourse.getScid());
                String sid = String.format("%-10s", studentCourse.getSid());
                String sname = String.format("%-8s", studentCourse.getSname());
                String tid = String.format("%-8s", studentCourse.getTid());
                String tname = String.format("%-6s", studentCourse.getTname());
                String cid = String.format("%-8s", studentCourse.getCid());
                String cname = String.format("%-10s", studentCourse.getCname());
                System.out.println(id + sid + sname + tid + tname + cid + cname);
            }
        } else {
            System.out.println("无选课记录，请录入！");
        }
    }
}
